package com.allen.apollo.controller;

import com.google.common.collect.Maps;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;

/**
  * @Description:描述
  * @Version v1.0.0
  * @author Administrator
  * @date 2022/5/22 19:41
  */
@RestController
public class IndexController {

    private static final String URL="http://localhost:8081/apollodemo";

    @Resource
    private RestTemplate restTemplate;

    /******post调用方式要注意的点：传参需要使用MultiValueMap将参数封装好，与get方式不同，get方式是用HashMap封装的。post方式传参不需要在url中使用占位符。***/


    @GetMapping("/queryConfigForGet")
    public String queryConfigForGet(){
        HashMap<Object, Object> map = Maps.newHashMap();
        map.put("namespace","application");
        String url = URL+"/getConfig?namespace={namespace}";
        return restTemplate.getForObject(url, String.class, map);
    }

    @GetMapping("/queryConfigForPost")
    public String queryConfigForPost(){
        LinkedMultiValueMap<String, String> valueMap = new LinkedMultiValueMap<>();
        valueMap.add("namespace","application");
        valueMap.add("key","author");
        String url = URL+"/getConfigByKey";
        return restTemplate.postForObject(url, valueMap,String.class );
    }
}
